package com.workcell.workdata.im.component.redis;

import com.workcell.workdata.im.entity.User;
import com.workcell.workdata.im.entity.proto.UserProto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class UserRedisTemplate extends RedisTemplate<String, User> implements RedisSerializer<User> {

	private static final String CACHE_PREFIX = "USER_";

	@Autowired
	public UserRedisTemplate(RedisConnectionFactory connectionFactory) {
		StringRedisSerializer stringSerializer = new StringRedisSerializer();

		setKeySerializer(stringSerializer);
		setHashKeySerializer(stringSerializer);

		setValueSerializer(this);
		setHashValueSerializer(this);

		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	public void save(User user) {
		String key = CACHE_PREFIX + user.getId();
		super.boundValueOps(key).set(user);
	}

	public User get(long uid) {
		String key = CACHE_PREFIX + uid;
		return super.boundValueOps(key).get();
	}

	public void remove(long uid) {
		String key = CACHE_PREFIX + uid;
		super.delete(key);
	}
	@Override
	public byte[] serialize(User user) throws SerializationException {
		UserProto.Model.Builder builder = UserProto.Model.newBuilder();

		builder.setId(user.getId());
		builder.setPassword(user.getPassword());
		builder.setName(user.getName());
		builder.setGender(user.getGender());
		builder.setTimestamp(user.getCreateTime().getTime());
		builder.setState(user.getState());

		if (user.getTelephone() != null){
			builder.setTelephone(user.getTelephone());
		}

		if (user.getEmail() != null){
			builder.setEmail(user.getEmail());
		}

		if (user.getMotto() != null){
			builder.setMotto(user.getMotto());
		}

		if (user.getOrganizationId() != null){
			builder.setOrganizationId(user.getOrganizationId());
		}

		return builder.build().toByteArray();
	}

	@Override
	public User deserialize(byte[] bytes) throws SerializationException {

		try {
			UserProto.Model proto = UserProto.Model.parseFrom(bytes);
			User user = new User();
			user.setId(proto.getId());
			user.setName(StringUtils.trimToNull(proto.getName()));
			user.setTelephone(StringUtils.trimToNull(proto.getTelephone()));
			user.setPassword(StringUtils.trimToNull(proto.getPassword()));
			user.setEmail(StringUtils.trimToNull(proto.getEmail()));
			user.setGender((byte) proto.getGender());
			user.setMotto(StringUtils.trimToNull(proto.getMotto()));
			user.setCreateTime(new Date(proto.getTimestamp()));
			user.setState((byte) proto.getState());
			user.setOrganizationId(proto.getOrganizationId());
			return user;

		} catch (Exception error) {
			return null;
		}
	}

}
